package com.sy.muldatasource.action;

import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sy.muldatasource.dao.one.TestS1Reposity;
import com.sy.muldatasource.dao.two.TestS2Reposity;
import com.sy.muldatasource.pojo.one.TestS1;
import com.sy.muldatasource.pojo.two.TestS2;
import com.sy.muldatasource.service.TestService;



@Controller
@RequestMapping("/")
public class TestController {
	@Autowired
	private TestS1Reposity s1Reposity;
	@Autowired
	private TestS2Reposity s2Reposity;
	@Autowired
	private TestService testService;
	
	@GetMapping("test")
    @ResponseBody
	public String jIndex(Model model) {
		Iterable<TestS1> s1s = s1Reposity.findAll();
		Iterator<TestS1> iterator = s1s.iterator();
		Integer count = 0;
		while(iterator.hasNext()) {
			iterator.next();
			count++;
		}
		System.out.println(String.format("数据源one获取的条数为%d", count));
		
		Iterable<TestS2> s2s = s2Reposity.findAll();
		Iterator<TestS2> iterator2 = s2s.iterator();
		count = 0;
		while(iterator2.hasNext()) {
			iterator2.next();
			count++;
		}
		System.out.println(String.format("数据源two获取的条数为%d", count));
		return "调用成功";
	}
	
	
	@GetMapping("saveSuccess")
	@ResponseBody
    public String tx() {
		testService.saveSuccess();
		return "调用成功";
    }
	
	@GetMapping("saveError")
	@ResponseBody
    public String tx2() {
		try {
			testService.saveError();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "调用成功";
    }

}
